package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.starlink.common.PageQueryRequest;
import com.starlink.common.utils.AssertUtil;
import com.starlink.mapper.MallPeripheryOrderDetailsMapper;
import com.starlink.model.dtomapper.MallBooksOrderDetailsDtoMapper;
import com.starlink.model.domain.MallBooksOrderDetailsDO;
import com.starlink.mapper.MallBooksOrderDetailsMapper;
import com.starlink.model.request.MallBooksOrderDetailsRequest;
import com.starlink.model.util.MallBooksOrderDetailsConvert;
import com.starlink.model.vo.BookReadingRecordVO;
import com.starlink.model.vo.MallBooksOrderDetailsVO;
import com.starlink.model.vo.mall.MallOrderDetailListVO;
import com.starlink.service.MallOrderDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

import static com.starlink.utils.IDUtils.getId;
import static com.starlink.utils.PageUtils.getOrderBy;

/**
 * @author zhanghong
 */
@Service
public class MallOrderDetailsServiceImpl extends ServiceImpl<MallBooksOrderDetailsMapper, MallBooksOrderDetailsDO> implements MallOrderDetailsService {
    @Autowired
    private MallBooksOrderDetailsMapper mallBooksOrderDetailsMapper;
    @Autowired
    private MallPeripheryOrderDetailsMapper permityOrderDetailsMapper;
    @Override
    public MallBooksOrderDetailsVO getByKey(long id) {
        MallBooksOrderDetailsVO mallBooksOrderDetailsVO = MallBooksOrderDetailsDtoMapper.INSTANCE.do2VO(getById(id));
        return mallBooksOrderDetailsVO;
    }

    @Override
    public PageInfo<MallBooksOrderDetailsVO> pageQuery(MallBooksOrderDetailsRequest request, Pageable pageable) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<MallBooksOrderDetailsDO> list = mallBooksOrderDetailsMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<MallBooksOrderDetailsVO> mallBooksOrderDetailsVOList = MallBooksOrderDetailsConvert.buildMallBooksOrderDetailsVoList(list);
        return new PageInfo(mallBooksOrderDetailsVOList);
    }

    @Override
    public List<MallBooksOrderDetailsVO> listQuery(MallBooksOrderDetailsRequest request) {
        List<MallBooksOrderDetailsDO> list = mallBooksOrderDetailsMapper.selectDOList(MallBooksOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        List<MallBooksOrderDetailsVO> mallBooksOrderDetailsVOS = MallBooksOrderDetailsConvert.buildMallBooksOrderDetailsVoList(list);
        return mallBooksOrderDetailsVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(MallBooksOrderDetailsRequest request) {
        request.setId(getId());
        save(MallBooksOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(MallBooksOrderDetailsRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(MallBooksOrderDetailsDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        mallBooksOrderDetailsMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public List<BookReadingRecordVO> pageQueryNot(Long userId, Pageable pageable, PageQueryRequest request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize(), getOrderBy(pageable.getSort())).setReasonable(true);
        List<BookReadingRecordVO> readingRecordVOS = mallBooksOrderDetailsMapper.pageQueryNot(userId);
        return readingRecordVOS;
    }

    @Override
    public void updateRecord(Long bookId, Long orderId) {
        mallBooksOrderDetailsMapper.updateRecord(bookId,orderId);
    }

    @Override
    public Map<String, Long> selectQuantity(Long orderId,Integer type) {
        return mallBooksOrderDetailsMapper.selectQuantity(orderId,type);
    }

    @Override
    public Long selectBookSpecificationId(Long orderId) {
        return mallBooksOrderDetailsMapper.selectSpecificationId(orderId);
    }

    @Override
    public Long selectRimSpecificationId(Long orderId) {
        return permityOrderDetailsMapper.selectSpecificationId(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBookOrderDetaile(Long orderId) {
        mallBooksOrderDetailsMapper.deleteBookOrderDetaile(orderId);
    }

    @Override
    public MallOrderDetailListVO selectBookOrderDetail(Long orderId) {
        return mallBooksOrderDetailsMapper.selectBookOrderDetail(orderId);
    }

    @Override
    public MallOrderDetailListVO selectRimOrderDetail(Long orderId) {
        return permityOrderDetailsMapper.selectRimOrderDetail(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateIsRefunds(Long orderId) {
        permityOrderDetailsMapper.updateIsRefunds(orderId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deletedByOrderIds(String stringIds) {
        mallBooksOrderDetailsMapper.deletedByOrderIds(Convert.toStrArray(stringIds));
    }
}